package com.ksm.client.document.view;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor.Ignore;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Singleton;
import com.ksm.client.document.TypeDocumentProperties;
import com.ksm.client.document.presenter.DocumentSearchPresenter.IDocumentSearchView;
import com.ksm.client.document.presenter.IDocumentSearchPresenter;
import com.ksm.transfer.dto.TypeDocumentDTO;
import com.sencha.gxt.core.client.XTemplates;
import com.sencha.gxt.data.shared.LabelProvider;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.Component;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.ComboBox;
import com.sencha.gxt.widget.core.client.form.DateField;
import com.sencha.gxt.widget.core.client.form.FormPanel;
import com.sencha.gxt.widget.core.client.form.NumberField;
import com.sencha.gxt.widget.core.client.form.NumberPropertyEditor;
import com.sencha.gxt.widget.core.client.form.NumberPropertyEditor.DoublePropertyEditor;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.info.Info;

@Singleton
public class DocumentSearchView implements IDocumentSearchView, IsWidget {

	private IDocumentSearchPresenter presenter;

	interface DocumenSearchViewUiBinder extends UiBinder<Component, DocumentSearchView> {
	}

	private static DocumenSearchViewUiBinder uiBinder = GWT.create(DocumenSearchViewUiBinder.class);

	private final TypeDocumentProperties typeProperties = GWT
		.<TypeDocumentProperties> create(TypeDocumentProperties.class);

	interface ComboBoxTemplates extends XTemplates {
		@XTemplate("<div qtip=\"{name}\" qtitle=\"State Slogan\">{symbol}</div>")
		SafeHtml state(String name, String symbol);
	}

	@UiField(provided = true)
	NumberPropertyEditor<Double> doublePropertyEditor = new DoublePropertyEditor();
	@UiField(provided = true)
	NumberFormat numberFormat = NumberFormat.getFormat("0.00");

	// editor fields
	@UiField
	@Ignore
	ComboBox<TypeDocumentDTO> comboBoxType;
	@UiField
	TextField interlocuteur;
	@UiField
	NumberField<Double> minAmount;
	@UiField
	NumberField<Double> maxAmount;
	@UiField
	DateField beginDate;
	@UiField
	DateField endDate;

	@UiField(provided = true)
	ListStore<TypeDocumentDTO> typeListStore = new ListStore<TypeDocumentDTO>(typeProperties.key());
	@UiField(provided = true)
	LabelProvider<TypeDocumentDTO> typeLabelProvider = typeProperties.nameLabel();

	@UiField(provided = true)
	FormPanel form = new FormPanel();

	private final Widget uiWidget;

	public DocumentSearchView() {

		uiWidget = uiBinder.createAndBindUi(this);
		// initComboType(TestData.getSomeStocks());
	}

	@Override
	public Widget asWidget() {
		return uiWidget;
	}

	@Override
	public void initComboType(List<TypeDocumentDTO> elements) {
		typeListStore.addAll(elements);
		comboBoxType.setValue(elements.get(0));
		addHandlersForEventObservation(comboBoxType, typeLabelProvider);
	}

	@UiHandler("reset")
	public void resetClicked(SelectEvent event) {
		form.reset();
	}

	@UiHandler("search")
	public void searchClicked(SelectEvent event) {
		presenter.findDocument();
	}

	private <T> void addHandlersForEventObservation(ComboBox<T> combo, final LabelProvider<T> labelProvider) {
		combo.addValueChangeHandler(new ValueChangeHandler<T>() {

			@Override
			public void onValueChange(ValueChangeEvent<T> event) {
				Info.display(
					"Value Changed",
					"New value: "
							+ (event.getValue() == null ? "nothing" : labelProvider.getLabel(event.getValue()) + "!"));
			}
		});
		combo.addSelectionHandler(new SelectionHandler<T>() {

			@Override
			public void onSelection(SelectionEvent<T> event) {
				Info.display(
					"State Selected",
					"You selected "
							+ (event.getSelectedItem() == null ? "nothing" : labelProvider.getLabel(event
								.getSelectedItem()) + "!"));
			}
		});
	}

	@Override
	public IDocumentSearchPresenter getPresenter() {
		return this.presenter;
	}

	@Override
	public void setPresenter(IDocumentSearchPresenter presenter) {
		this.presenter = presenter;

	}

	@Override
	public TextField getInterlocuteur() {
		return interlocuteur;
	}

	@Override
	public void setInterlocuteur(TextField interlocuteur) {
		this.interlocuteur = interlocuteur;
	}

	@Override
	public NumberField<Double> getMinAmount() {
		return minAmount;
	}

	@Override
	public void setMinAmount(NumberField<Double> minAmount) {
		this.minAmount = minAmount;
	}

	@Override
	public NumberField<Double> getMaxAmount() {
		return maxAmount;
	}

	@Override
	public void setMaxAmount(NumberField<Double> maxAmount) {
		this.maxAmount = maxAmount;
	}

	@Override
	public DateField getBeginDate() {
		return beginDate;
	}

	@Override
	public void setBeginDate(DateField beginDate) {
		this.beginDate = beginDate;
	}

	@Override
	public DateField getEndDate() {
		return endDate;
	}

	@Override
	public void setEndDate(DateField endDate) {
		this.endDate = endDate;
	}

	@Override
	public ComboBox<TypeDocumentDTO> getComboBoxType() {
		return comboBoxType;
	}

	@Override
	public void setComboBoxType(ComboBox<TypeDocumentDTO> comboBoxType) {
		this.comboBoxType = comboBoxType;
	}

}
